package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.stark.imgedit.utils.Matrix3;
import com.stark.imgedit.view.imagezoom.ImageViewTouchBase;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.databinding.ActivityClipsFileBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;

public class ClipsFileActivity extends BaseAc<ActivityClipsFileBinding> {
    public static Bitmap mImgBitmap;

    @Override
    protected int onCreate() {
        return R.layout.activity_clips_file;
    }

    @Override
    protected void initView() {
        mDataBinding.ivClipsFileBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivNext.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivNext://下一步
                clipsImg();
                break;
        }
    }

    @Override
    protected void initData() {
        mDataBinding.imageViewTouch.setImageBitmap(mImgBitmap);
        mDataBinding.imageViewTouch.setDisplayType(ImageViewTouchBase.DisplayType.FIT_TO_SCREEN);
        mDataBinding.imageViewTouch.setScaleEnabled(false);// 禁用缩放
        mDataBinding.imageViewTouch.post(new Runnable() {
            @Override
            public void run() {
                RectF bitmapRect = mDataBinding.imageViewTouch.getBitmapRect();
                mDataBinding.cropImgView.setCropRect(bitmapRect);
            }
        });
    }

    //裁剪图片
    private void clipsImg() {
        showDialog(getString(R.string.clips_ing));
        RxUtil.create(new RxUtil.Callback<Bitmap>() {
            @Override
            public void doBackground(ObservableEmitter<Bitmap> emitter) {
                RectF cropRect = mDataBinding.cropImgView.getCropRect();// 剪切区域矩形
                Matrix touchMatrix = mDataBinding.imageViewTouch.getImageViewMatrix();
                float[] data = new float[9];
                touchMatrix.getValues(data);// 底部图片变化记录矩阵原始数据
                Matrix3 cal = new Matrix3(data);// 辅助矩阵计算类
                Matrix3 inverseMatrix = cal.inverseMatrix();// 计算逆矩阵
                Matrix m = new Matrix();
                m.setValues(inverseMatrix.getValues());
                m.mapRect(cropRect);// 变化剪切矩形

                Bitmap resultBit = Bitmap.createBitmap(mImgBitmap,
                        (int) cropRect.left, (int) cropRect.top,
                        (int) cropRect.width(), (int) cropRect.height());
                emitter.onNext(resultBit);
            }

            @Override
            public void accept(Bitmap bitmap) {
                dismissDialog();
                SingleResultActivity.start(ClipsFileActivity.this, bitmap);
            }
        });
    }

}